package cn.com.shopec.erp.stock.dao.impl;

import org.hibernate.Query;
import org.hibernate.transform.Transformers;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.stereotype.Repository;

import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.orm.impl.HibernateEntityDao;
import cn.com.shopec.erp.common.utils.DateUtil;
import cn.com.shopec.erp.stock.dao.IStockInDao;
import cn.com.shopec.erp.stock.entity.StockIn;
import cn.com.shopec.erp.stock.vo.StockInVo;
import cn.com.shopec.erp.system.common.UserUtils;

/**
 * 
 * @ClassName: StockInDaoImpl.java
 * @Description: 入库信息Dao接口实现类
 * @author: zengxuezhou
 * @date: 2016年11月28日 下午2:24:02
 */
@Repository
public class StockInDaoImpl extends HibernateEntityDao<StockIn> implements IStockInDao {

	@Override
	public StockInVo queryWarehouseAndSupplier(String stockInNo) {
		String sql = "SELECT  " + " TW.WH_NAME whName," + " TSI.SUP_NAME supName " + " FROM TBL_STOCKIN AS TS"
				+ " LEFT JOIN TBL_SUPPLIER_INFO AS TSI ON TS.SUP_NO = TSI.CODE"
				+ " LEFT JOIN TBL_WAREHOUSE AS TW ON TS.WH_NO = TW.WH_NO " + " WHERE STOCKIN_NO = ? limit 0,1";
		Query query = getSession().createSQLQuery(sql);
		query.setParameter(0, stockInNo).setResultTransformer(Transformers.aliasToBean(StockInVo.class));
		StockInVo stockIn = (StockInVo) query.uniqueResult();
		return stockIn;
	}
	
	@Override
	public void updateStockInForApprove(String stockInNo) {
		String hql = "update StockIn as p set p.stockInState = ?,p.updater = ?,p.updateTime = ? where p.stockInNo = ?";
		getSession().createQuery(hql).setInteger(0, SZSCMConstant.STOCK_STATE_YES)
				.setString(1, UserUtils.getLoginUserName()).setDate(2, DateUtil.getCurrentDate())
				.setString(3, stockInNo).executeUpdate();
	}

	@Override
	public StockInVo getStockByNo(String stockNo) {
		String sql = "SELECT" + " S.ID AS id," + " S.STOCKIN_NO AS stockInNo," + " S.STOCKIN_TYPE AS stockInType,"
				+ " S.STOCKIN_STATE AS stockInState," + " S.STOCKIN_TYPE_NO AS stockInTypeNo," + " S.WH_NO AS whNo,"
				+ " S.PUR_NO AS purNo," + " S.SUP_NO AS supNo," + " S.DELETED_FLAG AS deletedFlag,"
				+ " TW.WH_NAME AS whName," + " TSI.SUP_NAME AS supName," + " TPO.PONAME AS purName"
				+ " FROM TBL_STOCKIN S" + " LEFT JOIN TBL_WAREHOUSE TW ON TW.WH_NO = S.WH_NO"
				+ " LEFT JOIN TBL_SUPPLIER_INFO TSI ON TSI.CODE = S.SUP_NO"
				+ " LEFT JOIN TBL_PURCHASE_ORDER TPO ON TPO.PO_NO = S.PUR_NO"
				+ " WHERE TW.DELETE_FLAG = 1 AND (TSI.DELETE_FLAG = 1 OR S.SUP_NO  IS NULL) AND S.STOCKIN_NO = ?";
		Query query = getSession().createSQLQuery(sql).addScalar("id", StandardBasicTypes.STRING)
				.addScalar("deletedFlag", StandardBasicTypes.INTEGER).addScalar("stockInNo", StandardBasicTypes.STRING)
				.addScalar("stockInTypeNo", StandardBasicTypes.STRING).addScalar("whNo", StandardBasicTypes.STRING)
				.addScalar("purNo", StandardBasicTypes.STRING).addScalar("supNo", StandardBasicTypes.STRING)
				.addScalar("whName", StandardBasicTypes.STRING).addScalar("supName", StandardBasicTypes.STRING)
				.addScalar("purName", StandardBasicTypes.STRING).addScalar("stockInType", StandardBasicTypes.INTEGER)
				.addScalar("stockInState", StandardBasicTypes.INTEGER);
		query.setParameter(0, stockNo).setResultTransformer(Transformers.aliasToBean(StockInVo.class));
		StockInVo stockInVo = (StockInVo) query.uniqueResult();
		return stockInVo;
	}
}
